/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */

package space.game.world;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.media.opengl.GL3bc;
import jinngine.math.Matrix4;
import space.gl.RenderContext;
import space.model.Drawable;
import space.model.MdlEmpty;
import space.model.MdlObject;

/**
 *
 * @author Pierre
 */
public class WldChild implements Drawable {

    final Matrix4 transform;
    Drawable rep;
    final List<Drawable> childs = new ArrayList<Drawable>();

    public WldChild(Matrix4 transfrom, MdlObject object, Map<String, Drawable> lib) {
        transform = transfrom;
        rep = object.representation();
        for (MdlObject o : object.getChilds()) {
            childs.add(new WldChild(o.getMatrix(), o, lib));
        }
        for (MdlEmpty e : object.empties()) {
            if (e.groupName.isEmpty() == false) {
                childs.add(new WldChild(e.matrix, (MdlObject) lib.get(e.groupName), lib));
            }
        }
    }

    public Drawable optimize(GL3bc gl) {
        rep = rep.optimize(gl);
        final ArrayList<Drawable> ochild = new ArrayList<Drawable>();
        for (Drawable drawable : childs) {
            ochild.add(drawable.optimize(gl));
        }
        childs.clear();
        childs.addAll(ochild);
        
        return this;
    }

    public void draw(RenderContext cr) {
        cr.compose(transform);
        rep.draw(cr.create());

        for (Drawable md : childs) {
            md.draw(cr.create());
        }


    }

    public void dispose(GL3bc gl) {
    }
}
